package com.novartis.nims.util;

/*---------------------------------------------------------------
*  Copyright 2005 by the Radiological Society of North America
*
*  This source software is released under the terms of the
*  RSNA Public License (http://mirc.rsna.org/rsnapubliclicense)
*----------------------------------------------------------------*/

import java.awt.BorderLayout;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

/**
 * An extension of JPanel containing a Scrollable JEditorPane set to display
 * HTML in a JScrollPane, with the rendered width tracking the width of the
 * JPanel. This provides the type of interface normally seen in a browser.
 */
@SuppressWarnings("serial")
public class HtmlJPanel extends JPanel {

	/** The text editor - provided for direct access to the JEditorPane methods. */
	public JEditorPane editor;
	/** The scroll pane - provided for direct access to the JScrollPane methods. */
	public JScrollPane scrollPane;

	/**
	 * Class constructor creating a Scrollable HTML panel with no text.
	 */
	public HtmlJPanel() {
		this("");
	}

	/**
	 * Class constructor creating a Scrollable HTML panel with an initial text string.
	 * @param text the initial text string.
	 */
	public HtmlJPanel(String text) {
		super();
		editor = new JEditorPane("text/html",text);
		editor.setEditable(false);
		this.setLayout(new BorderLayout());
		scrollPane = new JScrollPane(editor);
		scrollPane.getVerticalScrollBar().setUnitIncrement(25);
		scrollPane.getHorizontalScrollBar().setUnitIncrement(15);
		this.add(scrollPane,BorderLayout.CENTER);
	}

	/**
	 * Replace the editor's current text with new text.
	 * @param text the replacement text string.
	 */
	public void setText(String text) {
		editor.setText(text);
	}

	/**
	 * Return the editor's current text.
	 */
	public String getText() {
		return editor.getText();
	}

	/**
	 * Scroll to the top of the text.
	 */
	public void scrollToTop() {
		//make sure we update the position after any other scrollbar updates
		//that may have been invoked elsewhere have arrived, e.g. an auto scroll
		//after editor text has changed
		
		Runnable doScroll = new Runnable() {
            public void run() {
            	JScrollBar sb = scrollPane.getVerticalScrollBar();
        		sb.setValue(sb.getMinimum());
            }
        };

        SwingUtilities.invokeLater(doScroll);
	}

	/**
	 * Scroll to the bottom of the text.
	 */
	public void scrollToBottom() {
		Runnable doScroll = new Runnable() {
            public void run() {
            	JScrollBar sb = scrollPane.getVerticalScrollBar();
        		sb.setValue(sb.getMaximum());
            }
        };

        SwingUtilities.invokeLater(doScroll);
	}

	/**
	 * Scroll to the left of the text.
	 */
	public void scrollToLeft() {
		Runnable doScroll = new Runnable() {
            public void run() {
            	JScrollBar sb = scrollPane.getHorizontalScrollBar();
        		sb.setValue(sb.getMinimum());
            }
        };

        SwingUtilities.invokeLater(doScroll);
	}

	/**
	 * Scroll to the right of the text.
	 */
	public void scrollToRight() {
		Runnable doScroll = new Runnable() {
            public void run() {
            	JScrollBar sb = scrollPane.getHorizontalScrollBar();
        		sb.setValue(sb.getMaximum());
            }
        };

        SwingUtilities.invokeLater(doScroll);
	}

}

